﻿#include "PoseSettingDialog.h"
#include "ui_PoseSettingDialog.h"

#include "PoseSettingData.h"
#include <QIntValidator>
#include "IdaWidgetHelper.h"
#include "InfomationGraph3DBasical.h"
#include "BaseModule/IBaseSignal.h"
#include <QDebug>

namespace pst
{
    PoseSettingDialog::PoseSettingDialog(QWidget* parent)
        : IdaDialogFrame(parent)
        , ui(new Ui::PoseSettingDialog())
        , m_currentData{ nullptr }
    {
        ui->setupUi(this);

        setWindowTitle(tr("偏移量设置"));

        //只能输入 double 数字
        QDoubleValidator* doubleValidator = new QDoubleValidator(-1e6, 1e6, 6, this);
        ui->lineEdit_transX->setValidator(doubleValidator);
        ui->lineEdit_transY->setValidator(doubleValidator);
        ui->lineEdit_transZ->setValidator(doubleValidator);
        ui->lineEdit_rotationTheta->setValidator(doubleValidator);
        ui->lineEdit_rotationPhi->setValidator(doubleValidator);

        IdaWidgetHelperInstance->setMainButtonStyle(ui->pushButton_confirm); // 将确定设置为主按钮
    }

    PoseSettingDialog::~PoseSettingDialog()
    {
        delete ui;
    }

    void PoseSettingDialog::updateSetting(InfomationGraph3DBasical* info)
    {
        m_currentData = info;
        if (info == nullptr || m_currentData->m_poseSettingData == nullptr)
        {
            qDebug() << "PoseSettingDialog::updateSetting data is nullptr";
            return;
        }
        auto data = m_currentData->m_poseSettingData;
        ui->lineEdit_transX->setText(QString::number(data->getTranslationX()));
        ui->lineEdit_transY->setText(QString::number(data->getTranslationY()));
        ui->lineEdit_transZ->setText(QString::number(data->getTranslationZ()));
        ui->lineEdit_rotationTheta->setText(QString::number(data->getRotationTheta()));
        ui->lineEdit_rotationPhi->setText(QString::number(data->getRotationPhi()));
    }

    void PoseSettingDialog::on_pushButton_confirm_clicked()
    {
        if (m_currentData == nullptr)
        {
            qDebug() << "PoseSettingDialog::on_pushButton_confirm_clicked m_currentData is nullptr";
            this->close();
        }

        auto data = m_currentData->m_poseSettingData;
        data->setTranslationX(ui->lineEdit_transX->text().toDouble());
        data->setTranslationY(ui->lineEdit_transY->text().toDouble());
        data->setTranslationZ(ui->lineEdit_transZ->text().toDouble());
        data->setRotationTheta(ui->lineEdit_rotationTheta->text().toDouble());
        data->setRotationPhi(ui->lineEdit_rotationPhi->text().toDouble());

        int windowId = m_currentData->m_graph->getWindowID();
        int infoId = m_currentData->m_informationID;
        double translationX = ui->lineEdit_transX->text().toDouble();
        double translationY = ui->lineEdit_transY->text().toDouble();
        double translationZ = ui->lineEdit_transZ->text().toDouble();
        double rotationTheta = ui->lineEdit_rotationTheta->text().toDouble();
        double rotationPhi = ui->lineEdit_rotationPhi->text().toDouble();

        QStringList _pyCodes{};
        _pyCodes += QString("threedsurfaceplotmoveresultdatasetting1 = PostProcessing.ThreeDSurfacePlotMoveResultDataSetting()");
        _pyCodes += QString("threedsurfaceplotmoveresultdatasetting1.setWindowId(%1)").arg((int)windowId);
        _pyCodes += QString("threedsurfaceplotmoveresultdatasetting1.setInfomationId(%1)").arg((int)infoId);
        _pyCodes += QString("threedsurfaceplotmoveresultdatasetting1.setMoveData(%1,%2,%3,%4,%5)").arg(translationX).arg(translationY).arg(translationZ).arg(rotationTheta).arg(rotationPhi);
        _pyCodes += QString("threedsurfaceplotmoveresultdatasetting1.saveThreeDSurfacePlotMoveResultDataSetting()");

        emit IBaseSignalInstance->signal_execPYCode(_pyCodes, true, false);

        emit signal_updateActorPose(m_currentData);
        this->close();
    }

    void PoseSettingDialog::on_pushButton_cancel_clicked()
    {
        this->close();
    }
}
